package com.itheima.controller;

import com.itheima.dto.SmsCouponParam;
import com.itheima.pojo.SmsCoupon;
import com.itheima.service.SmsCouponService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@RestController
@RequestMapping("coupon")
public class SmsCouponController {
    @Autowired
    private SmsCouponService smsCouponService;

    /**
     * 添加优惠券
     * @param couponParam 封装的请求体
     * @return
     */
    @PostMapping("create")
    public Result create(@RequestBody SmsCouponParam couponParam){

        if (couponParam.getEndTime()==null||couponParam.getStartTime()==null||couponParam.getEnableTime()==null) {
            return Result.errorMy("请设置时间");
        }else {
            //      时间判断
            if (couponParam.getEndTime().compareTo(couponParam.getStartTime())<0){
                return Result.errorMy("有效期的开始时间必须在结束时间前");
            }
            if (couponParam.getEnableTime().compareTo(couponParam.getStartTime())<0){
                return Result.errorMy("领取时间必须在开始时间前");
            }
        }

//      优惠面额的判断
        if(couponParam.getMinPoint().compareTo(couponParam.getAmount())<0){
            return Result.errorMy("使用门槛必须大于优惠面额");
        }
//      总发行量与每人限领的判断
        if(couponParam.getPublishCount().compareTo(couponParam.getPerLimit())<0){
            return Result.errorMy("总发行量必须大于每人的限领数");
        }
        smsCouponService.create(couponParam);
        return Result.success(null);
    }

    /**
     * 删除优惠券
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public Result deleteById(@PathVariable("id")Integer id){
        smsCouponService.deleteById(id);
        return Result.success(null);
    }

    /**
     * 修改优惠券
     * @param id
     * @param smsCouponParam
     * @return
     */
    @PostMapping("update/{id}")
    public Result updateById(@PathVariable("id")Integer id,@RequestBody SmsCouponParam smsCouponParam){
        if (smsCouponParam.getEndTime()==null||smsCouponParam.getStartTime()==null||smsCouponParam.getEnableTime()==null) {
            return Result.errorMy("请设置时间");
        }else {
            //      时间判断
            if (smsCouponParam.getEndTime().compareTo(smsCouponParam.getStartTime())<0){
                return Result.errorMy("有效期的开始时间必须在结束时间前");
            }
            if (smsCouponParam.getEnableTime().compareTo(smsCouponParam.getStartTime())<0){
                return Result.errorMy("领取时间必须在开始时间前");
            }
        }

//      优惠面额的判断
        if(smsCouponParam.getMinPoint().compareTo(smsCouponParam.getAmount())<0){
            return Result.errorMy("使用门槛必须大于优惠面额");
        }
//      总发行量与每人限领的判断
        if(smsCouponParam.getPublishCount().compareTo(smsCouponParam.getPerLimit())<0){
            return Result.errorMy("总发行量必须大于每人的限领数");
        }

        smsCouponService.updateById(id,smsCouponParam);
        return Result.success(null);
    }


    /**
     * 根据优惠券名称和类型分页获取优惠券列表,,模糊查询  分页
     * @param name
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public Result list(String name,Integer type,@RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
        PageResultVo<SmsCoupon> resultVo=smsCouponService.list(name,type,pageNum,pageSize);
        return Result.success(resultVo);
    }

    /**
     * 获取单个优惠券详细信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result getSmsCouponById(@PathVariable("id") Integer id){
        SmsCouponParam smsCouponParam=smsCouponService.getSmsCouponById(id);
        return Result.success(smsCouponParam);
    }
}
